﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Admin.Master" AutoEventWireup="True" CodeBehind="EditCustomDashboardGadget.aspx.cs" Inherits="Cms.Web.UI.Admin.EditCustomDashboardGadget" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script language="javascript" type="text/javascript" src="Admin/edit_area/edit_area_loader.js"></script>
    <script language="javascript" type="text/javascript" src="Admin/Js/Edit.js"></script>
    <script language="javascript" type="text/javascript">
        //<![CDATA[
        $(document).ready(function () {
            cms.Edit.ShowTabOverlays();
        });

        var codeEditorLoaded = false;
        var xmlEditorLoaded = false;

        function editorLoaded(id) {
            if (id == "txtCode")
                codeEditorLoaded = true;
            if (id == "txtXml")
                xmlEditorLoaded = true;
            if (codeEditorLoaded && xmlEditorLoaded) {
                editAreaLoader.setValue("txtXml", originalXml);
                editAreaLoader.setValue("txtCode", originalCode);
                cms.Edit.HideTabOverlays();
            }
            if (document.getElementById("ctl00_body_pnlProperties").style.display == "")
                document.getElementById("ctl00_body_txtFilename").focus();
            if (document.getElementById("ctl00_body_pnlXml").offsetLeft >= 0) {
                editAreaLoader.toggle("txtXml");
                editAreaLoader.toggle("txtXml");
            }
            if (document.getElementById("ctl00_body_pnlCode").offsetLeft >= 0) {
                editAreaLoader.toggle("txtCode");
                editAreaLoader.toggle("txtCode");
            }
        }

        editAreaLoader.init({
            id: "txtXml",
            syntax: "xml",
            start_highlight: true,
            allow_resize: "no",
            allow_toggle: false,
            toolbar: "undo,redo,change_smooth_selection,reset_highlight,highlight,syntax_selection",
            word_wrap: true,
            replace_tab_by_spaces: 4,
            EA_load_callback: "editorLoaded"
        });

        editAreaLoader.init({
            id: "txtCode",
            syntax: "html",
            start_highlight: true,
            allow_resize: "no",
            allow_toggle: false,
            toolbar: "undo,redo,change_smooth_selection,reset_highlight,highlight,syntax_selection",
            word_wrap: true,
            replace_tab_by_spaces: 4,
            EA_load_callback: "editorLoaded"
        });

        function updateCode() {
            txtXml = document.getElementById("txtXml");
            txtCode = document.getElementById("txtCode");
            //alert("Design before:\n" + txtDesign.value + "\n\n\nCode before:\n" + txtCode.value);
            txtXml.value = editAreaLoader.getValue("txtXml");
            document.getElementById("ctl00_body_hidXml").value = txtXml.value;
            txtCode.value = editAreaLoader.getValue("txtCode");
            document.getElementById("ctl00_body_hidCode").value = txtCode.value;
        }

        function setCodeEditorHeight() {
            var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
            var xmlContainer = document.getElementById("xmlContainer");
            var codeContainer = document.getElementById("codeContainer");
            var xmlHeight = viewportHeight - iLib.Pos.FindY(xmlContainer) - 65;
            xmlHeight = xmlHeight < 100 ? 100 : xmlHeight;
            xmlContainer.style.height = xmlHeight + "px";
            var codeHeight = viewportHeight - iLib.Pos.FindY(codeContainer) - 65;
            codeHeight = codeHeight < 100 ? 100 : codeHeight;
            codeContainer.style.height = codeHeight + "px";
        }

        function updatePanels(tab) {
            var pnlProperties = document.getElementById("ctl00_body_pnlProperties");
            var pnlXml = document.getElementById("ctl00_body_pnlXml");
            var pnlCode = document.getElementById("ctl00_body_pnlCode");
            pnlProperties.style.display = "none";
            var width = pnlXml.offsetWidth;
            pnlXml.style.position = "absolute";
            pnlXml.style.left = -10000 + "px";
            pnlXml.style.width = width + "px";
            pnlXml.style.top = "0px";
            pnlCode.style.position = "absolute";
            pnlCode.style.left = -10000 + "px";
            pnlCode.style.width = width + "px";
            pnlCode.style.top = "0px";
            switch (tab) {
                case "properties":
                    pnlProperties.style.display = "";
                    break;
                case "xml":
                    pnlXml.style.position = "";
                    pnlXml.style.width = "100%";
                    pnlXml.style.left = "0px";
                    break;
                case "code":
                    pnlCode.style.position = "";
                    pnlCode.style.width = "100%";
                    pnlCode.style.left = "0px";
                    break;
            }
        }

        var txtFilename;
        var txtXml;
        var txtCode;
        var originalFilename;
        var originalXml;
        var originalCode;

        function init() {
            setCodeEditorHeight();
            txtFilename = document.getElementById("ctl00_body_txtFilename");
            txtXml = document.getElementById("txtXml");
            txtCode = document.getElementById("txtCode");
            originalFilename = txtFilename.value;
            originalXml = document.getElementById("ctl00_body_hidXml").value;
            originalCode = document.getElementById("ctl00_body_hidCode").value;
            editAreaLoader.setValue("txtXml", originalXml);
            editAreaLoader.setValue("txtCode", originalCode);
        }

        function isDirty() {
            var value = originalFilename == "" ||
                        originalXml != txtXml.value ||
	                    originalCode != txtCode.value ||
                        originalFilename != txtFilename.value;
            document.getElementById("ctl00_tabs_hidIsDirty").value = value;
            return value;
        }
	    //]]>
	</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="buttons" runat="server">
    <asp:UpdatePanel ID="upnlButtons" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:LinkButton ID="lbSave" runat="server" OnClick="lbSave_Click" CssClass="orangeButton" OnClientClick="isDirty(); updateCode();"><span id="lblSave1" runat="server"><span id="lblSave2" runat="server"><img id="imgSave" runat="server" src="Admin/Img/icons/disk.png" alt="" /><span id="lblSave3" runat="server"><cms:LocalizedLiteral ID="llSave" runat="server" /></span></span></span></asp:LinkButton>
            <asp:hyperlink ID="hlCancel" runat="server" navigateUrl="Admin/DashboardGadgets.aspx" CssClass="orangeButton"><span id="lblCancel1" runat="server"><span id="lblCancel2" runat="server"><img id="imgCancel" runat="server" src="Admin/Img/icons/delete.png" alt="" /><span id="lblCancel3" runat="server"><cms:LocalizedLiteral ID="llCancel" runat="server" /></span></span></span></asp:hyperlink>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="tabs" runat="server">
    <asp:UpdatePanel ID="upnlTabs" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <cms:AdminTabStrip ID="tabStrip" runat="server" SelectedValue="properties" OnChange="tabStrip_Click">
                <cms:AdminTabItem ID="tiProperties" runat="server" value="properties" EnabledImage="Admin/Img/icons/page_white_gear.png"><cms:LocalizedLiteral ID="llPropertiesTab" runat="server" /></cms:AdminTabItem>
                <cms:AdminTabItem ID="tiCode" runat="server" value="code" EnabledImage="Admin/Img/icons/page_white_csharp.png"><cms:LocalizedLiteral ID="llCodeTab" runat="server" /></cms:AdminTabItem>
                <cms:AdminTabItem ID="tiXml" runat="server" value="xml" EnabledImage="Admin/Img/icons/page_white_world.png"><cms:LocalizedLiteral ID="llXmlTab" runat="server" /></cms:AdminTabItem>
            </cms:AdminTabStrip>
            <input type="hidden" id="hidIsDirty" runat="server" />
            <input type="hidden" id="hidSave" runat="server" />
            <input type="hidden" id="hidGadgetID" runat="server" />
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="body" runat="server">
    <asp:Literal ID="litIcon" runat="server" Visible="false" Text="Admin/Img/Icons/application_view_tile_edit.png" />

    <asp:Panel ID="pnlXml" runat="server" CssClass="pnlTab">
        <asp:UpdatePanel ID="upnlXml" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
            <ContentTemplate>
                <input type="hidden" id="hidXml" runat="server" />
                <input type="hidden" id="hidSaveXml" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <div id="xmlContainer" style="height: 400px;">
            <textarea id="txtXml" cols="10" rows="10" style="width: 100%; height: 100%;"></textarea>
        </div>
        <div class="pnlTabOverlay"></div>
    </asp:Panel>
    
    <asp:Panel ID="pnlCode" runat="server" CssClass="pnlTab">
        <asp:UpdatePanel ID="upnlCode" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
            <ContentTemplate>
                <input type="hidden" id="hidCode" runat="server" />
                <input type="hidden" id="hidSaveCode" runat="server" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <div id="codeContainer" style="height: 400px;">
            <textarea id="txtCode" cols="10" rows="10" style="width: 100%; height: 100%;"></textarea>
        </div>
        <div class="pnlTabOverlay"></div>
    </asp:Panel>
    
    <asp:Panel ID="pnlProperties" runat="server" CssClass="pnlTab">
        <asp:UpdatePanel ID="upnlProperties" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
            <ContentTemplate>
                <table class="editForm">
                    <tr>
                        <td><cms:LocalizedLiteral ID="llFilename" runat="server" />:</td>
                        <td>
                            <table border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="padding-right: 4px;"><asp:TextBox ID="txtFilename" runat="server" /></td>
                                    <td><strong>.ascx</strong></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
        <div class="pnlTabOverlay"></div>
    </asp:Panel>

    <script language="javascript">
        //<![CDATA[
        iLib.Dom.AddListener(window, "load", init);
        cms.Ajax.Init();
        cms.Ajax.AddEndRequestHandler(function () {
            editAreaLoader.setValue("txtXml", document.getElementById("ctl00_body_hidXml").value);
            editAreaLoader.setValue("txtCode", document.getElementById("ctl00_body_hidCode").value);
            cms.Edit.HideTabOverlays();
            if (document.getElementById("ctl00_body_pnlProperties").style.display == "")
                document.getElementById("ctl00_body_txtFilename").focus();
            if (document.getElementById("ctl00_body_pnlXml").offsetLeft >= 0) {
                editAreaLoader.toggle("txtXml");
                editAreaLoader.toggle("txtXml");
            }
            if (document.getElementById("ctl00_body_pnlCode").offsetLeft >= 0) {
                editAreaLoader.toggle("txtCode");
                editAreaLoader.toggle("txtCode");
            }
        });
        //]]>
    </script>
</asp:Content>
